<template>
  <div class="login_container">
    <div class="login-box">
      <div class="avatar-box">
        <img src="../../assets/images/logo.png" alt="" />
      </div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        label-width="80px"
        class="login-form"
        @keyup.13="loginSubmit('loginForm')"     
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-tubiao211"
          ></el-input>
        </el-form-item>
        <el-form-item label="密  码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="iconfont icon-mima"
          ></el-input>
        </el-form-item>
        <el-form-item class="login-btns">
          <el-button type="primary"  @click="loginSubmit('loginForm')"
            >登录</el-button
          >
          <el-button type="info" @click="resetForm('loginForm')"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      loadingConfig: {
        //加载层参数配置
        target: ".add-user-dialog",
        fullscreen: true,
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.1)",
        text: "拼命加载中",
      },
    };
  },
  methods: {
    //登录提交
    loginSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //打开加载层
          var loading = this.$loading.service(this.loadingConfig);
          this.$axios
            .post("/login", {
              username: this.loginForm.username,
              password: this.loginForm.password,
            })
            .then((res) => {
              if (res.meta.status === 200) {
                //登陆成功后
                //关闭加载层
                loading.close();
                this.$message({
                  message: res.meta.msg,
                  type: "success",
                });
                //存放登陆成功后返回的token
                localStorage.setItem("token", res.data.token);
                //跳转到主页
                this.$router.push("/welcome");
              } else {
                //关闭加载层
                loading.close();
                //登陆失败
                this.$message.error(res.meta.msg);
              }
            });
        }
      });
    },
    //重置表单
    resetForm: function (formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  position: relative;
  height: 100%;
  background-color: #2b4b6b;
}
.login-box {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 450px;
  height: 304px;
  background-color: #fff;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 10px #ddd;
}
.avatar-box {
  position: absolute;
  left: 50%;
  width: 130px;
  height: 130px;
  padding: 10px;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  transform: translateX(-50%) translateY(-50%);
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #efefef;
  }
}
.login-form {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 0 20px 0 10px;
  box-sizing: border-box;
}
.login-btns {
  text-align: right;
}
</style>